/*This is the code for running the analysis for the 1997-2019 data*/

/*	*****************************************************************************/
/*     File Name:	Studying Nonobviousness Part II.do						  	*/
/*     	Date:   	December 10, 2020											*/
/*      Author: 	Abigail Matthews and Jason Rantanen							*/
/*      Purpose:	Construction of Part II 1997-2019 Dataset, 					*/
/*					and analysis of Part II data			 					*/
/* 		Input files: Part II dataset 1997-2019.dta								*/
/*      			 appeals 2020-11-10.dta										*/
/*					 appeals 2021-02-24.dta										*/
/*	*****************************************************************************/


ssc install tsegen
ssc install asdoc

use "Part II dataset 1997-2019", clear

/*First define the dataset that we'll be using*/

gen coder2_dataset_2020 = 1
replace coder2_dataset_2020 = 0 if year==2020 | year==1996
replace coder2_dataset_2020 = 0 if docType=="Dupe" | docType=="Errata" | docType=="Order"
replace coder2_dataset_2020 = 0 if origin=="CFC"
replace coder2_dataset_2020 = 0 if ProceduralPosture==5 //These are preliminary injunctions
replace coder2_dataset_2020 = 0 if ProceduralPosture==9

replace caseDate = Date if caseDate==.

gen preKSR_cert = 1 if caseDate < date("6/27/2006","MDY")
gen postKSR_decision = 1 if caseDate > date("4/30/2007","MDY")
gen postRantanen_2013 = 1 if caseDate > date("4/30/2012","MDY")

replace year = year(caseDate) if year==.

tab docType postRantanen_2013 if coder2_dataset_2020==1
tab docType if coder2_dataset_2020==1 & postRantanen_2013 != 1 & postKSR_decision==1

keep if coder2_dataset_2020==1 

*********************************
**# Figure 1: NUMBER OF FEDERAL CIRCUIT DECISIONS INVOLVING § 103 IN APPEALS ARISING FROM DISTRICT COURTS BETWEEN 1997 AND 2019 **
tab year docType if coder2_dataset_2020==1

sort docType
encode docType, gen(DocType)
tab DocType
tab DocType, nolab
bysort year: egen opinion = count(DocType) if DocType==1
bysort year: egen rule36 = count(DocType) if DocType==2

label var opinion "Opinion"
label var rule36  "Rule 36"

graph bar (count) opinion rule36 if year <2020, over(year, label(angle(45) labsize(small))) legend(label(1 "Opinion") label(2 "Rule 36") region(lstyle(none))) stack scheme(s1color) title("") ytitle(Frequency) ylabel(0(5)35) intensity(*0.6) bar(1, fcolor(orange) lcolor(orange) lwidth(medium)) bar(2, bcolor(gray) lwidth(medium))

graph save "opinion.rule36.gph", replace


*********************************
**# Figure 3: 25-RECORD TRAILING AVERAGE OF FEDERAL CIRCUIT OBVIOUSNESS DECISIONS, 1996-2019 **

sort caseDate
gen ID=_n

tsset ID

bysort year: egen obvioustotal = count(CAFC_Result) if CAFC_Result <3
bysort year: egen obvious = count(CAFC_Result) if CAFC_Result==1 
	*replace obvious = 0 if obvious==.
bysort year: gen obviouspct = (obvious/obvioustotal) * 100

sort ID
tsegen ma = rowmean(obviouspct L(0/25).obviouspct) //Lag of 25 years
tssmooth ma ma1 = obviouspct, window(1 1 25) //another way to calculate rolling averages

tsline ma, sort xlabel(1 "1997" 37 "1999" 81 "2001" 118 "2003" 159 "2005" 200 "2007" 243 "2009" 303 "2011" 361 "2013" 428 "2015" 477 "2017" 521 "2019") ytitle(Percent) xtitle("") ylabel(0(10)100, grid) recast(area) scheme(s1color) 

graph save "25-record rolling average.gph", replace
graph save "25-record rolling average.png", replace



merge m:1 uniqueID using "appeals 2020-11-10.dta", keepusing(origin year docType duplicate) update


*********************************
**# Figure 2: OBVIOUS VS. NONOBVIOUS OUTCOMES AT THE FEDERAL CIRCUIT, 1997-2019 **

tab year CAFC_Result if coder2_dataset_2020==1, missing
bysort year: egen obvious_CAFC = count(CAFC_Result) if CAFC_Result==1
bysort year: egen nonobvious_CAFC = count(CAFC_Result) if CAFC_Result==2

graph bar obvious_CAFC nonobvious_CAFC if coder2_dataset_2020==1, over(year, label(angle(45) labsize(small))) legend(label(1 "Obvious") label(2 "Nonobvious") label(3 "Both") rows(1) region(lstyle(none))) ytitle("Frequency") title("") scheme(s1color) bar(1, bcolor(red)) bar(2, bcolor(green)) intensity(*0.7)

graph save "CAFC.obvious.nonobvious.gph", replace

*********************************
**#  Figure 4: PROCEDURAL POSTURE OF § 103 DECISIONS IN APPEALS ARISING FROM DISTRICT COURTS, 1997-2019 **

tab year ProceduralPosture if coder2_dataset_2020==1, missing

tab ProceduralPosture

bysort year: egen jury = count(ProceduralPosture) if ProceduralPosture==1
bysort year: egen bench = count(ProceduralPosture) if ProceduralPosture==2
bysort year: egen jmol = count(ProceduralPosture) if ProceduralPosture==3
bysort year: egen sj = count(ProceduralPosture) if ProceduralPosture==4
*bysort year: egen pi = count(ProceduralPosture) if ProceduralPosture==5
bysort year: egen mixed = count(ProceduralPosture) if ProceduralPosture==6
*bysort year: egen pto = count(ProceduralPosture) if ProceduralPosture==9

label var jury "jury"

*graph twoway bar jury bench jmol sj pi mixed pto year
 
graph bar jury bench jmol sj if year<2020, percent stack over(year, label(angle(45) labsize(small))) asyvars legend(rows(1) label(1 "Jury") label(2 "Bench") label(3 "JMOL") label(4 "Summary Judgment") region(lstyle(none))) scheme(s1color) bar(1, bcolor(gray))  bar(2, bcolor(blue))  bar(3, bcolor(orange))  bar(4, bcolor(gold)) ytitle("Percent") 

*********************************
**# Figure 5: DISTRICT COURT OUTCOMES REVIEWED AT THE FEDERAL CIRCUIT **

tab year PostureOutcome if coder2_dataset_2020==1, missing
bysort year: egen obvious_postureoutcome = count(PostureOutcome) if PostureOutcome==1
bysort year: egen nonobvious_postureoutcome = count(PostureOutcome) if PostureOutcome==2
bysort year: egen both_postureoutcome = count(PostureOutcome) if PostureOutcome==3

label var obvious_postureoutcome "Obvious"
label var nonobvious_postureoutcome "Nonobvious"
label var both_postureoutcome "Both"

graph bar obvious_postureoutcome nonobvious_postureoutcome if coder2_dataset_2020==1, over(year, label(angle(45) labsize(small))) legend(label(1 "Obvious") label(2 "Nonobvious") rows(1) region(lstyle(none))) ytitle("Frequency") title("") scheme(s1color) bar(1, bcolor(red)) bar(2, bcolor(green)) intensity(*0.7)

graph save "districtcourt.obvious.nonobvious.gph", replace


*********************************
**# Figure 6: PERCENTAGE OF DISTRICT COURT DECISIONS INVOLVING § 103 THE FEDERAL CIRCUIT AFFIRMED **

tab year DispositionCondensed if coder2_dataset_2020==1

tab PostureOutcome DispositionCondensed if coder2_dataset_2020==1 & preKSR_cert == 1
tab PostureOutcome DispositionCondensed if coder2_dataset_2020==1 & postRantanen_2013 != 1 & postKSR_decision==1
tab PostureOutcome DispositionCondensed if postRantanen_2013==1 & coder2_dataset_2020==1

tab PostureOutcome DispositionCondensed if coder2_dataset_2020==1 & caseDate>date("12/31/2014","MDY")

bysort year: egen affirmtotal = count(DispositionCondensed)
bysort year: egen affirmed = count(DispositionCondensed) if DispositionCondensed==1 
bysort year: gen affirm = (affirmed/affirmtotal) * 100

graph bar affirm if year<2020, over(year, label(angle(45))) title("") ytitle("Percent") ylabel( 0(10)100, angle(horizontal))  scheme(s1color) intensity(*.7)

graph save "103affirmed.gph", replace
graph save "103affirmed.png", replace



*********************************
**# Appendix D, Table D1:  CAFC_Result by author, 2013-2019 (active judges only)
drop if uniqueID==.  // Drops records for which there is no uniqueID, and thus no match to Compendium.
drop _merge
merge 1:1 uniqueID using "appeals 2020-11-10.dta", keepusing(opinion1Author)
drop if _merge==2
drop _merge

gen activeJudge=0
replace activeJudge=1 if opinion1Author=="Chen"
replace activeJudge=1 if opinion1Author=="Dyk"
replace activeJudge=1 if opinion1Author=="Hughes"
replace activeJudge=1 if opinion1Author=="Lourie"
replace activeJudge=1 if opinion1Author=="Moore"
replace activeJudge=1 if opinion1Author=="Newman"
replace activeJudge=1 if opinion1Author=="O'Malley"
replace activeJudge=1 if opinion1Author=="Prost"
replace activeJudge=1 if opinion1Author=="Reyna"
replace activeJudge=1 if opinion1Author=="Stoll"
replace activeJudge=1 if opinion1Author=="Taranto"
replace activeJudge=1 if opinion1Author=="Wallach"

rename activeJudge activeJudge_opinion1

tab opinion1Author CAFC_Result if year>=2013 & year<=2019 & activeJudge_opinion1==1 & coder2_dataset_2020==1

asdoc tab opinion1Author CAFC_Result if year>=2013 & year<=2019 & activeJudge_opinion1==1 & coder2_dataset_2020==1, label font(Times New Roman) fs(12) save(issue103_authorjudge.doc) replace title(\i)

*********************************
// Comparison with Issue_103 coding from Compendium
use "Part II dataset 1997-2019", clear

gen coder2_dataset_2020 = 1
replace coder2_dataset_2020 = 0 if year==2020 | year==1996
replace coder2_dataset_2020 = 0 if docType=="Dupe" | docType=="Errata" | docType=="Order"
replace coder2_dataset_2020 = 0 if origin=="CFC"
replace coder2_dataset_2020 = 0 if ProceduralPosture==5 //These are preliminary injunctions
replace coder2_dataset_2020 = 0 if ProceduralPosture==9

replace caseDate = Date if caseDate==.
replace year = year(caseDate) if year==.
keep if coder2_dataset_2020==1 

drop if uniqueID==.


merge 1:1 uniqueID using "appeals 2021-02-24.dta", gen(merge2) keepusing(origin year docType duplicate Issue_103) update
tab coder2_dataset, mi
replace coder2_dataset = 0 if coder2_dataset==.

keep if origin=="DCT" | origin=="PTO"
keep if docType=="Opinion"
drop if year==2020 | year==2021
drop if duplicate=="Yes"

sort Issue_103
encode Issue_103, gen(temp)
recode temp (1=0) (2=1)
label drop Issue_103
label define Issue_103 0 No 1 Yes
label values temp Issue_103

drop Issue_103
rename temp Issue_103

tab Issue_103 coder2_dataset_2020 if origin=="DCT"
tab coder2_dataset_2020
label define coder2_dataset_2020 1 Yes 0 No, replace
label values coder2_dataset_2020 coder2_dataset_2020
tab coder2_dataset_2020

label var Issue_103 "Fed. Cir. Appeals involving § 103 issue "
label var coder2_dataset_2020  "Compendium of Federal Circuit Decisions"

kap Issue_103 coder2_dataset_2020 if origin=="DCT"

**# TABLE 14: COMPARISON DATA ON § 103 CODING
asdoc tab Issue_103 coder2_dataset_2020 if origin=="DCT", label font(Times New Roman) fs(12) save(issue103coder2data.doc) replace title(\i)


**# TABLE 15: FEDERAL CIRCUIT DECISIONS IN APPEALS ARISING FROM THE DISTRICT COURTS AND USPTO THAT INVOLVED § 103.
label var year Year
label var origin "Origin of Case"
asdoc tab year origin if Issue_103==1, label font(Times New Roman) fs(12) save(appellatedecsec103.doc) replace title(\i)


